﻿<script type="text/javascript">
    $(function () {
        getButton("button");
    });

    function ui_button_init_list(toolbar) {
        $("#ui_button_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_button.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
            idField: 'id',
            sortName: 'sort',
            sortOrder: 'asc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                             { field: 'text', title: '按钮名称', width: 100 }
            ]],
            columns: [[
                       { field: 'code', title: '标识码',  width: 120 },
                       { field: 'iconCls', title: '图标', width: 150 },
                       { field: 'sort', title: '排序', sortable: true, width: 80 },
                       { field: 'addDate', title: '添加时间',width: 130 },
                       { field: 'description', title: '描述', width: 250 }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,
            //toolbar: [{
            //    text:'添加',
            //    iconCls:'icon-add',
            //    handler: function () { ui_button_add();}
            //},{
            //    text:'修改',
            //    iconCls:'icon-edit',
            //    handler: function () { ui_button_edit();}
            //},'-',{
            //    text:'删除',
            //    iconCls:'icon-remove',
            //    handler:function(){ui_button_delete()}
            //}],
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_button_edit();
            }
        });
    }

    function ui_button_add() {
        $("<div/>").dialog({
            id: "ui_button_add_dialog",
            href: "html/ui_button_edit.html",
            title: "添加按钮",
            iconCls: 'icon-add',
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_button_add_btn",
                text: '添 加',
                iconCls: 'icon-add',
                title: "添加按钮",
                handler: function () {
                    $("#ui_button_editform").form("submit", {
                        url: "ashx/rm_button.ashx",
                        onSubmit: function (param) {
                            $('#ui_button_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_button_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_button_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_button_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_button_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_button_name").focus();
            },
            onClose: function () {
                $("#ui_button_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_button_edit() {       
        var rows = $("#ui_button_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的按钮或者双击某个按钮");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_button_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_button_edit_dialog",
            href: "html/ui_button_edit.html",
            title: "修改用户",
            iconCls: 'icon-edit',
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_button_edit_btn",
                iconCls: 'icon-edit',
                text: '修 改',
                handler: function () {
                    $("#ui_button_editform").form("submit", {
                        url: "ashx/rm_button.ashx",
                        onSubmit: function (param) {
                            $('#ui_button_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_button_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_button_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_button_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_button_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {                
                $("#ui_button_id").val(row.id);
                $("#ui_button_name").textbox("setValue",row.text);
                $("#ui_button_code").textbox("setValue", row.code);
                $("#ui_button_icon").textbox("setValue", row.iconCls);
                $('#ui_button_sort').numberspinner('setValue', row.sort);
                $("#ui_button_description").val(row.description);
            },
            onClose: function () {
                $("#ui_button_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_button_delete() {
        var rows = $("#ui_button_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的按钮");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个按钮？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_button.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_button_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
</script>
<div id="ui_button_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="ui_button_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
